<template>
  <div class="common-layout">
    <el-container>
      <el-header><Header></Header></el-header>
      <!-- <el-container> -->
      <!-- <el-main>
        </el-main> -->
      <!-- <el-aside width="200px">Aside</el-aside> -->
      <!-- </el-container> -->
    </el-container>
    <!-- <ul v-infinite-scroll="load" class="infinite-list" style="overflow: auto;height: 70%;">
    <li v-for="i in count" :key="i" class="infinite-list-item">{{ i }}</li>
  </ul> -->
  </div>
</template>

<script>
// import Header from './header.vue';

import Header from './header.vue';
import Main from './main.vue';
export default {
  components: { Header, Main },
  mounted() {
    // 添加滚动条滚动的监听事件
    window.addEventListener('scroll', this.scroll, false);
  },
  beforeDestroy() {
    // 删除滚动条滚动的监听事件
    window.removeEventListener('scroll', this.scroll, false);
  },
  data() {
    return {
      count: 0
    };
  },
  methods: {
    load() {
      this.count += 4;
    },
    // 获取热销商品
    getHotProduct() {
      // 当前是否正在加载，如果是正在加载就取消加载数据
      if (this.loading) {
        this.page--;
        return;
      }
      if (this.productList.length >= this.totalCount) return;
      // 赋值表示正在加载
      this.loading = true;
      getHots({
        limit: this.limit,
        page: this.page
      })
        .then(({ data }) => {
          if (data.code === 0) {
            this.totalCount = data.page.totalCount;
            this.totalPage = data.page.totalPage;
            this.productList = this.productList.concat(data.page.list);
          }
          // 表示加载完成
          this.loading = false;
        })
        .catch((res) => {
          console.log(res);
        });
    },
    scroll() {
      window.onscroll = () => {
        // 整个页面的高度
        const scrollHeight = document.body.scrollHeight;
        // 当前可视区的顶部到页面顶部的高度，||是做兼容处理的
        const scrollTop = document.body.scrollTop || document.documentElement.scrollTop;
        // 当前可视区的高度
        const clientHeight = document.documentElement.clientHeight;
        // 可视区底部到页面底部的高度，即滚动条与底部的距离
        const instance = scrollHeight - scrollTop - clientHeight;
        // 当滚动条与底部的距离小于100时就触发加载数据
        if (instance < 100) {
          this.page++;
          this.getHotProduct();
        }
      };
    }
  }
};
</script>

<style scoped>
.common-layout {
  height: 100%;
  width: 100%;
}
.el-container {
  height: 100%;
  width: 100%;
}

.el-header {
  width: 100%;
  height: 20%;
}

.el-aside {
  background-color: #f4f5;
  color: #333;
  text-align: center;
  line-height: 200px;
  width: 30%;
}

.el-main {
  background-color: #f4f5f5;
  /* color: #333;  */
  text-align: center;
  line-height: 160px;
  width: 70%;
}
.infinite-list {
  height: 300px;
  padding: 0;
  margin: 0;
  list-style: none;
}
.infinite-list .infinite-list-item {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 50px;
  background: var(--el-color-primary-light-9);
  margin: 10px;
  color: var(--el-color-primary);
}
.infinite-list .infinite-list-item + .list-item {
  margin-top: 10px;
}
</style>
